*********************************************
* Replication dofile of the paper:
* Agricultural Transformation and Farmers' Expectations:  Experimental Evidence from Uganda 
* Jacopo Bonan, Harounan Kazianga, Mariapia Mendola
* August 2025
***********************************************
global path  ""

******************************************
***Figure 1
******************************************
use "${path}\dataset_crop_panel_may25" ,clear

global cond1 "if attriter==0"

set scheme s1mono

foreach x in yield price  { //

distcomp  w_`x'exp_mean_st_base_ $cond1 & report_`x'_exp_base_==1, by(treatment_status) saving(cdf_`x'_pool_base, replace)  gropttwoway(ytitle(ECDF) xtitle("Oilseed `x' expectations, z-score", size(small)) legend(cols(3) label(1 "Treat") label( 2 "Control" ) ) )

distcomp  w_`x'exp_mean_st_ $cond1 & report_`x'_exp_==1, by(treatment_status) saving(cdf_`x'_pool_end, replace)  gropttwoway(ytitle(ECDF) xtitle("Oilseed `x' expectations, z-score", size(small)) legend(cols(3) label(1 "Treat") label( 2 "Control" ) ) )
}

*net install grc1leg,from( http://www.stata.com/users/vwiggins/)

grc1leg cdf_yield_pool_base.gph cdf_price_pool_base.gph , graphregion(fcolor(white)) ///
      title("Baseline expectations", size(Medium)) saving(cdf_pool_base, replace) legendfrom(cdf_yield_pool_base.gph)

grc1leg cdf_yield_pool_end.gph cdf_price_pool_end.gph , graphregion(fcolor(white)) ///
      title("Endline expectations", size(Medium)) saving(cdf_pool_end, replace) legendfrom(cdf_yield_pool_end.gph)

grc1leg cdf_pool_base.gph cdf_pool_end.gph, graphregion(fcolor(white)) r(2) ///
    saving(cdf_pool_tot, replace) legendfrom(cdf_pool_end.gph)
	

**********************************************************************
* Table 1. Respondent and household summary statistics and balance
**********************************************************************
use "${path}\dataset_crop_panel_may25" ,clear

global contr_1 resp_head_base sex_resp_base resp_read_base resp_write_base  resp_noedu_base resp_primary_base resp_secondary_base  wealth_index_base tot_plot_size_both_base n_plot_both_base  index_klk_know_all_base index_klk_welfare_base

global tot $contr_1 

local replace replace
foreach y of global tot{

sum `y'  if treatment_status==0&oilseed_id==1
local avg_cont=r(mean)
local sd_contr=r(sd)

reg `y' treatment_status i.distr_code if oilseed_id==1, cluster(sub_county_code_n)
outreg2  using "${path_tables}descr_1_jan21.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont', SD, (`sd_contr') )   dec(3)
local replace 
}

mat drop _all
foreach y of global tot{
local lab_`y': variable label  `y'

sum `y'  if treatment_status==0 &oilseed_id==1
local avg_cont=r(mean)
local sd_contr=r(sd)

reg `y' treatment_status i.distr_code if oilseed_id==1, cluster(sub_county_code_n)

matrix coef=(e(b))
matrix var=(e(V))

matrix lineX= nullmat(lineX) \ ( (`avg_cont'), (coef[1,1]) \ (`sd_contr') , sqrt(var[1,1]) )

}

drop _all
svmat lineX

local i 1
ge variables=""
order variables

foreach y of global tot{
replace variables="`lab_`y''" in `i'
local i=`i'+2
}

forvalues i=1/2{

ge nline`i'=string(lineX`i', "%9.3f") if lineX`i'<100
replace nline`i'=string(lineX`i', "%9.1f") if lineX`i' >=100
}

* give names to columns
rename nline1 Control_Mean_SD
rename nline2 ITT_SE

*tostring Control_Mean_SD ITT_SE, replace
ge Control_Mean_SD2=""
ge ITT_SE2=""

local nnn=_N

forval i=1/`nnn'{
local gg=Control_Mean_SD in `i'
replace Control_Mean_SD2="`gg'" in `i' if mod(`i',2)==1 
replace Control_Mean_SD2="(`gg')" in `i' if mod(`i',2)==0 

local jj=ITT_SE in `i'
replace ITT_SE2="`jj'" in `i' if mod(`i',2)==1 
replace ITT_SE2="(`jj')" in `i' if mod(`i',2)==0 
}

keep variables Control_Mean_SD2 ITT_SE2

save "${path_tables}descr_gen_outcomes_dfe", replace

**********************************************************************
* Table 2 Oilseed-specific summary statistics and balance
**********************************************************************

use "${path}\dataset_crop_panel_may25" ,clear

global f1 report_yield_exp_  w_yieldexp_mean_ report_price_exp_  w_priceexp_mean_  optimist_yield_  pessimist_yield_ optimist_price_  pessimist_price_  d_adopt_both_ sh_adopt_uni_  index_klk_input_  index_klk_work_ index_klk_prod_ index_klk_sale_

global tot  $f1

local replace replace

mat drop _all

forval c=1/4{
foreach y of global tot{
local lab_`y': variable label  `y'

sum `y'base_  if treatment_status==0&oilseed_id==`c'
local avg_cont=r(mean)
local sd_contr=r(sd)

reg `y'base_  treatment_status i.distr_code  if oilseed_id==`c', cluster(sub_county_code_n)
matrix coef=(e(b))
matrix var=(e(V))

matrix lineX= nullmat(lineX) \ ( (`avg_cont'), (coef[1,1]) \ (`sd_contr') , sqrt(var[1,1]) )

}
}


drop _all
svmat lineX

local i 1
ge variables=""
order variables

foreach y of global tot{
replace variables="`lab_`y''" in `i'
local i=`i'+2
}

forvalues i=1/2{
ge nline`i'=string(lineX`i', "%9.3f") if lineX`i'<100
replace nline`i'=string(lineX`i', "%9.1f") if lineX`i' >=100

ge vnline`i'=""
}

ge Control_Mean_SD2=""
ge ITT_SE2=""

local nnn=_N

forval i=1/2{

forval h=1/`nnn'{
local gg=nline`i' in `h'
replace vnline`i'="`gg'" in `h' if mod(`h',2)==1 
replace vnline`i'="(`gg')" in `h' if mod(`h',2)==0 
}
}

keep variables vnline*
rename vnline1 Control_Mean
rename vnline2 ITT

save "${path_tables}descr_gen__cond_outcomes2", replace

**********************************************************************
* Table 3. Samples reporting oilseed expectations at baseline and endline
**********************************************************************
use "${path}\dataset_crop_panel_may25" ,clear

global cond1 "if attriter==0"


forval i=1/4{
ta report_yield_exp_ report_yield_exp_base_ $cond1 & oilseed_id==`i', ce
ta report_price_exp_ report_price_exp_base_ $cond1 & oilseed_id==`i', ce
	
bys d_adopt_both_:  ta report_yield_exp_ report_yield_exp_base_ $cond1 & oilseed_id==`i'
bys d_adopt_both_:  ta report_price_exp_ report_price_exp_base_ $cond1 & oilseed_id==`i'
}

**********************************************************************
* Table 4 Program impact on the probability of reporting yield and price expectations (extensive margin)
**********************************************************************
*Panel A: main effects

use "${path}\dataset_crop_panel_may25"  ,clear

global cond1 "if attriter==0"

local replace replace
forval i=1/4{
sum report_yield_exp_  if treatment_status==0&attriter==0 & oilseed_id==`i'
local avg_cont=r(mean)

reg report_yield_exp_  treatment_status   report_yield_exp_base_  i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}report_itt2.xls",  label keep(treatment_status report_yield_exp_base_  )   adds(Control mean, `avg_cont') dec(3)  `replace'
local replace
}

***general oilseed (pooled model)
sum report_yield_exp_ if treatment_status==0&attriter==0
local avg_cont=r(mean)

reg report_yield_exp_  treatment_status   report_yield_exp_base_  i.distr_code i.oilseed_id  $cond1, vce(cluster sub_county_code_n)
outreg2  using "${path_tables}report_itt2.xls",  label keep(treatment_status report_yield_exp_base_  )    adds(Control mean, `avg_cont') dec(3)  `replace'


*price
local replace replace
forval i=1/4{
sum report_price_exp_  if treatment_status==0&attriter==0 & oilseed_id==`i'
local avg_cont=r(mean)
local avg_cont=r(mean)

local replace
reg report_price_exp_  treatment_status   report_price_exp_base_  i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}report_itt2.xls",  label keep(treatment_status report_price_exp_base_)    adds(Control mean, `avg_cont') dec(3)  `replace'
local replace
}

***general oilseed (pooled model)

sum report_price_exp_ if treatment_status==0&attriter==0
local avg_cont=r(mean)

reg report_price_exp_  treatment_status   report_price_exp_base_   i.distr_code i.oilseed_id  $cond1, vce(cluster sub_county_code_n)
outreg2  using "${path_tables}report_itt2.xls",  label keep(treatment_status report_price_exp_base_  )   adds(Control mean, `avg_cont') dec(3)  `replace'

**Panel B: heterogeneous effects (optimist as reference category)

use "${path}\dataset_crop_panel_may25"  ,clear

ge optimist= optimist_yield_base_
ge pessimist= pessimist_yield_base_
ge notrep=notrep_yield_base_

local replace replace
forval i=1/4{

reg report_yield_exp_  treatment_status##optimist treatment_status##pessimist  i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_rep22.xls",  label  dec(3)  `replace' keep(1.treatment_status 1.treatment_status#1.optimist 1.treatment_status#1.pessimist) adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' ) ctitle(Report, crop `i') 
local replace
}


**pooled
reg report_yield_exp_  treatment_status##optimist treatment_status##pessimist i.distr_code i.oilseed_id   $cond1, vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 


outreg2  using "${path_tables}het_rep22.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist) adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' )  ctitle(Report, pooled) 


**price
cap drop optimist
ge optimist=optimist_price_base_
cap drop pessimist
ge pessimist=pessimist_price_base_
cap drop notrep
ge notrep=notrep_price_base_

forval i=1/4{

reg report_price_exp_  treatment_status##optimist treatment_status##pessimist    i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_rep22.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist)  adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' )  ctitle(Report, Crop `i') 
}

**pooled sample

reg report_price_exp_  treatment_status##optimist treatment_status##pessimist i.distr_code  i.oilseed_id  $cond1, vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_rep22.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist)  adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' ) ctitle(Report, Pooled)

**********************************************************************
* Table 5. Program impact on the levels of yield and price expectations (extensive margin)
**********************************************************************

*Panel A. Main effects

use "${path}\dataset_crop_panel_may25"  ,clear

* set baseline expectations to zero when missing
replace w_priceexp_mean_base_=0 if report_price_exp_base_==0
replace w_yieldexp_mean_base_=0 if report_yield_exp_base_==0


global cond1 "if attriter==0"

local replace replace 

forval i=1/4{

sum w_yieldexp_mean_ if treatment_status==0&attriter==0 & oilseed_id==`i'
local avg_cont=r(mean)

reg w_yieldexp_mean_  treatment_status  w_yieldexp_mean_base_ report_yield_exp_base_ i.distr_code  $cond1 & oilseed_id==`i'  , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}levels_itt_25.xls",  label keep(treatment_status)    adds(Control mean, `avg_cont') dec(3)  `replace' 
local replace
}

*pooled
sum w_yieldexp_mean_ if treatment_status==0&attriter==0 
local avg_cont=r(mean)

reg w_yieldexp_mean_  treatment_status  w_yieldexp_mean_base_ report_yield_exp_base_ i.distr_code i.oilseed_id  $cond1  , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}levels_itt_25.xls",  label keep(treatment_status)    adds(Control mean, `avg_cont') dec(3)  `replace' 

*price
forval i=1/4{
	
sum w_priceexp_mean_ if treatment_status==0&attriter==0 & oilseed_id==`i'
local avg_cont=r(mean)

reg w_priceexp_mean_  treatment_status  w_priceexp_mean_base_   i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}levels_itt_25.xls",  label keep(treatment_status)  adds(Control mean, `avg_cont' ) dec(3)  `replace' 
local replace
}

*pooled
sum w_priceexp_mean_ if treatment_status==0&attriter==0 
local avg_cont=r(mean)

reg w_priceexp_mean_  treatment_status  w_priceexp_mean_base_   i.distr_code i.oilseed_id  $cond1 , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}levels_itt_25.xls",  label keep(treatment_status)  adds(Control mean, `avg_cont' ) dec(3)  `replace' 

**************************
*Panel B. Heterogeneous effects
**************************
*yields
use "${path}\dataset_crop_panel_may25"  ,clear

global cond1 "if attriter==0"

ge notrep=notrep_yield_base_
*cap drop optimist
ge optimist= optimist_yield_base_
*cap drop pessimist
ge pessimist= pessimist_yield_base_

local replace replace 
forval i=1/4{
	reg w_yieldexp_mean_  treatment_status##optimist treatment_status##pessimist   i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_lev25.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist)  adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' ) 
local replace
}

*pooled
reg w_yieldexp_mean_  treatment_status##optimist treatment_status##pessimist   i.distr_code i.oilseed_id  $cond1, vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_lev25.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist)  adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' ) 

**price

forval i=1/4{

cap drop notrep
ge notrep=notrep_price_base_

		cap drop optimist
ge optimist=optimist_price_base_

cap drop pessimist
ge pessimist=pessimist_price_base_

reg w_priceexp_mean_  treatment_status##optimist treatment_status##pessimist  i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_lev25.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist)  adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' )  ctitle(Levels, Crop `i')

local replace
}

*pooled
 reg w_priceexp_mean_  treatment_status##optimist treatment_status##pessimist  i.distr_code  i.oilseed_id  $cond1, vce(cluster sub_county_code_n)

lincom 1.treatment_status + 1.treatment_status#1.optimist
local sum1=r(estimate)
local f_sum1= r(p) 

lincom 1.treatment_status + 1.treatment_status#1.pessimist 
local sum2=r(estimate)
local f_sum2= r(p) 

lincom 1.treatment_status#1.optimist - 1.treatment_status#1.pessimist 
local sum3=r(estimate)
local f_sum3= r(p) 

outreg2  using "${path_tables}het_lev25.xls",  label  dec(3)  `replace' keep(1.treatment_status  1.treatment_status#1.optimist 1.treatment_status#1.pessimist)  adds(TE optimist, `sum1', p-val TE optimist , `f_sum1', TE pessimist, `sum2', pval TE pessimist, `f_sum2', Pval difference, `f_sum3' )  ctitle(Levels, pooled)


****************************
***TAble 6 - Program impact on oilseed adoption
*******************************
use "${path}\dataset_crop_panel_may25"  ,clear

global v1  d_adopt_both_ sh_adopt_uni_   

local cond1 "if attriter==0&oilseed_id==1"
local cond2 "if attriter==0&oilseed_id==2"
local cond3 "if attriter==0&oilseed_id==3"
local cond4 "if attriter==0&oilseed_id==4"

local replace replace

***PANEL A: Adoption

forval i=1/4{
sum d_adopt_both_  `cond`i'' &treatment_status==0
local avg_cont=r(mean)

reg d_adopt_both_  treatment_status   d_adopt_both_base_   i.distr_code   `cond`i'' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_adopt_main.xls", `replace' keep(treatment_status) label   dec(3) adds(Control mean, `avg_cont')
local replace
}

sum d_adopt_both_  if attriter==0&treatment_status==0
local avg_cont=r(mean)

reg d_adopt_both_ treatment_status   d_adopt_both_base_    i.distr_code i.oilseed_id if attriter==0, vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_adopt_main.xls", `replace' keep(treatment_status) label   dec(3) adds(Control mean, `avg_cont')


**PANEL B: Land %
forval i=1/4{
sum sh_adopt_uni_  `cond`i'' &treatment_status==0
local avg_cont=r(mean)

reg sh_adopt_uni_  treatment_status   sh_adopt_uni_base_   i.distr_code  `cond`i'' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_adopt_main.xls", `replace' keep(treatment_status) label    dec(3) adds(Control mean, `avg_cont')
local replace
}

sum sh_adopt_uni_  if attriter==0& treatment_status==0
local avg_cont=r(mean)

reg sh_adopt_uni_ treatment_status   sh_adopt_uni_base_   i.distr_code i.oilseed_id if attriter==0, vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_adopt_main.xls", `replace' keep(treatment_status) label   dec(3) adds(Control mean, `avg_cont')


********************
***Table 7. Program impact on household outcomes & MHT
*******************
use "${path}\dataset_crop_panel_may25" ,clear

global cond1 "if attriter==0&oilseed_id==1"

global all index_klk_know_all index_klk_welfare

matrix drop _all
local replace replace
foreach y of global all{
local lab_`y': variable label  `y'

reg `y'   treatment_status   `y'_base  i.distr_code  $cond1, vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_indexes.xls", `replace' keep(treatment_status) label   dec(3)

local replace
local t = _b[treatment_status]/_se[treatment_status]
local p =2*ttail(e(df_r),abs(`t'))

matrix line =nullmat(line)\ ( `p')
}

***crop-specific outcomes 
use "${path}\dataset_crop_panel_may25" ,clear

global all index_klk_adoption_ index_klk_input_ index_klk_work_ index_klk_prod_ index_klk_sale_  

global all2 index_klk_know_all index_klk_welfare index_klk_adoption_ index_klk_input_ index_klk_work_ index_klk_prod_ index_klk_sale_ 

global cond1 "if attriter==0"

local replace 
foreach y of global all{
local lab_`y': variable label  `y'

reg `y'   treatment_status   `y'base_ i.distr_code i.oilseed_id  $cond1, vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_indexes.xls", `replace' keep(treatment_status) label   dec(3)

local t = _b[treatment_status]/_se[treatment_status]
local p =2*ttail(e(df_r),abs(`t'))

matrix lineb =nullmat(lineb)\ ( `p')
}

matrix linec=line\lineb
matrix list linec

global all2 index_klk_know_all index_klk_welfare index_klk_adoption_ index_klk_input_ index_klk_work_ index_klk_prod_ index_klk_sale_  index_klk_exp_
drop _all

svmat linec

rename linec1 p_value
*encode variables, gen(n)

qqvalue  p_value , me(hochberg ) qv(q_val)
format q_val %9.3f

save "${path_tables}table_q_values", replace


***************
*APPENDIX
***************

****************************************
*Figures C.3. - C.6
****************************************
use "${path}\dataset_crop_panel_may25" , clear

global cond1 "if attriter==0"

lab val treatment_status treat
lab def treat 0 "Control" 1 "Treat"

local c1 "Soyabean"
local c2 "Sunflower"
local c3 "Sesame"
local c4 "Groundnut"


foreach x in yield price{
forval i=1/4{
la var w_`x'exp_mean_base_ "`c`i'' `x' expectations"
la var w_`x'exp_mean_ "`c`i'' `x' expectations"

**baseline
distcomp w_`x'exp_mean_base_ $cond1 & report_`x'_exp_base_==1 & oilseed_id==`i', by(treatment_status) saving(cdf_`x'_base_, replace)  gropttwoway(ytitle(ECDF) xtitle("`c`i''", size(small)) legend(cols(3) label(1 "Treat") label( 2 "Control" ) ) )    // title("`c`i''", size(small))

**endline
distcomp w_`x'exp_mean_ $cond1  & report_`x'_exp_==1 & oilseed_id==`i', by(treatment_status) saving(cdf_`x'_, replace) gropttwoway(ytitle(ECDF) xtitle("`c`i''", size(small)) legend(cols(3) label(1 "Treat") label( 2 "Control" ) ) )
}
}

grc1leg cdf_yield_base_1.gph cdf_yield_base_2.gph cdf_yield_base_3.gph cdf_yield_base_4.gph, graphregion(fcolor(white)) ///
      title("Yield expectations - Baseline", size(Medium)) saving(cdf_yield_base, replace) legendfrom(cdf_yield_base_4.gph)

grc1leg cdf_price_base_1.gph cdf_price_base_2.gph cdf_price_base_3.gph cdf_price_base_4.gph, graphregion(fcolor(white)) ///
      title("Price expectations - Baseline", size(Medium)) saving(cdf_price_base, replace) legendfrom(cdf_price_base_4.gph)

 
grc1leg cdf_yield_1.gph cdf_yield_2.gph cdf_yield_3.gph cdf_yield_4.gph, graphregion(fcolor(white)) ///
      title("Yield expectations - Endline", size(Medium)) saving(cdf_yield_end, replace) legendfrom(cdf_yield_1.gph)  
	  
grc1leg cdf_price_1.gph cdf_price_2.gph cdf_price_3.gph cdf_price_4.gph, graphregion(fcolor(white)) ///
      title("Price expectations - Endline", size(Medium)) saving(cdf_price_end, replace) legendfrom(cdf_price_2.gph)  

*********************************
**Table C.1 Analysis of attrition
**********************************
use "${path}\dataset_crop_panel_may25" ,clear
*resp_read_base resp_write_base
global contr_1 resp_head_base sex_resp_base resp_noedu_base resp_primary_base resp_secondary_base  wealth_index_base tot_plot_size_both_base n_plot_both_base 

***differential attrition
sum attriter  if treatment_status==0&oilseed_id==1
local avg_cont=r(mean)

reg attriter  treatment_status  i.distr_code if oilseed_id==1, cluster(sub_county_code_n)
outreg2  using "${path_tables}attrtion_jan21.xls", replace  label  adds(Control mean, `avg_cont')   dec(3) keep(treatment_status )

reg attriter  treatment_status d_adopt_oilseed_both_base $contr_1 i.distr_code if oilseed_id==1, cluster(sub_county_code_n)
outreg2  using "${path_tables}attrtion_jan21.xls", append  label  adds(Control mean, `avg_cont')   dec(3) keep(treatment_status d_adopt_oilseed_both_base $contr_1 )

**selective attrition
rename d_adopt_oilseed_both_base d_ad_os_base
rename tot_plot_size_both_base plot_size
rename n_plot_both_base nn_plot_base

global contr_1 resp_head_base sex_resp_base resp_noedu_base resp_primary_base resp_secondary_base  wealth_index_base plot_size nn_plot_base  

 estimates clear
 local replace replace

foreach y in d_ad_os_base $contr_1{
	
	reg `y' treatment_status i.distr_code if oilseed_id==1 & attriter==0 
	
	estimate store attr1_`y'
	
	outreg2  using "${path_tables}attrtion_sel.xls", `replace'  label  dec(3) keep(treatment_status) ctitle(`y',non-attriter) 
	
	reg `y' treatment_status i.distr_code if  oilseed_id==1 & attriter==1 
	
		estimate store attr2_`y'

	outreg2  using "${path_tables}attrtion_sel2.xls", `replace'  label  dec(3) keep(treatment_status) ctitle(`y', attriter) 
	local replace

	}

forval i=1/2{
	suest attr`i'_*, vce(cluster sub_county_code_n)
	test ([attr`i'_d_ad_os_base_mean]treatment_status==0)([attr`i'_resp_head_base_mean]treatment_status==0)([attr`i'_sex_resp_base_mean]treatment_status==0)([attr`i'_resp_noedu_base_mean]treatment_status==0)([attr`i'_resp_primary_base_mean]treatment_status==0)([attr`i'_resp_secondary_base_mean]treatment_status==0)([attr`i'_wealth_index_base_mean]treatment_status==0)([attr`i'_plot_size_mean]treatment_status==0)([attr`i'_nn_plot_base_mean]treatment_status==0)

}

** determinants of attrition
global contr_1 resp_head_base sex_resp_base resp_noedu_base resp_primary_base resp_secondary_base  wealth_index_base plot_size nn_plot_base 

 estimates clear
 local replace replace
foreach y in treatment_status d_ad_os_base $contr_1{
	
	reg `y' attriter i.distr_code if  oilseed_id==1  
	
	estimate store attr3_`y'
	
	outreg2  using "${path_tables}attrtion_sel3.xls", `replace'  label  dec(3) keep(attriter) ctitle(`y') 
	
	local replace

	}

forval i=3/3{
	suest attr`i'_*, vce(cluster sub_county_code_n)
	test ([attr`i'_treatment_status_mean]attriter==0) ([attr`i'_d_ad_os_base_mean]attriter==0)([attr`i'_resp_head_base_mean]attriter==0)([attr`i'_sex_resp_base_mean]attriter==0)([attr`i'_resp_noedu_base_mean]attriter==0)([attr`i'_resp_primary_base_mean]attriter==0)([attr`i'_resp_secondary_base_mean]attriter==0)([attr`i'_wealth_index_base_mean]attriter==0)([attr`i'_plot_size_mean]attriter==0)([attr`i'_nn_plot_base_mean]attriter==0)
}

***************************************
** Table C.2 and C.3 Determinants of yield and price expectations
**************************************
use "${path}\dataset_crop_panel_may25" , clear

local cond1 "if attriter==0"

global contr_1 resp_head_base sex_resp_base resp_noedu_base resp_primary_base resp_secondary_base  wealth_index_base  tot_plot_size_both_base n_plot_both_base  epsedec  iirr  

local replace replace
forval i=1/4{

reg report_yield_exp_base_  d_adopt_both_base_ sh_adopt_uni_base_ report_price_exp_base_ n_report_yield_base_  d_fertil_both_base d_pestic_both_base d_impr_seed_both_base ///
d_sale_all_both_base tech_water_base tech_post_harvest_base  $contr_1 i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}det_exp.xls",  label    dec(3)  `replace'
local replace

reg w_yieldexp_mean_base_  d_adopt_both_base_ sh_adopt_uni_base_ report_price_exp_base_ n_report_yield_base_ d_fertil_both_base d_pestic_both_base d_impr_seed_both_base ///
d_sale_all_both_base tech_water_base tech_post_harvest_base  $contr_1 i.distr_code   $cond1 & report_yield_exp_base_==1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}det_exp.xls",  label   dec(3)  `replace'
}

forval i=1/4{

reg report_price_exp_base_  d_adopt_both_base_ sh_adopt_uni_base_   report_yield_exp_base_ n_report_price_base_  d_fertil_both_base d_pestic_both_base d_impr_seed_both_base ///
d_sale_all_both_base tech_water_base tech_post_harvest_base  $contr_1 i.distr_code   $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}det_exp.xls",  label   dec(3)  `replace'
local replace

reg w_priceexp_mean_base_  d_adopt_both_base_ sh_adopt_uni_base_ report_yield_exp_base_ n_report_price_base_  d_fertil_both_base d_pestic_both_base d_impr_seed_both_base ///
d_sale_all_both_base tech_water_base tech_post_harvest_base  $contr_1 i.distr_code   $cond1 & report_price_exp_base_==1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}det_exp.xls",  label   dec(3)  `replace'
}

***********************************
**Table C.4 Correlation between populations' and own expectations
**********************************
use "${path}\dataset_crop_panel_may25" , clear

global cond1 "if attriter==0"
global contr_1 resp_head_base sex_resp_base resp_read_base resp_write_base  resp_noedu_base resp_primary_base resp_secondary_base  wealth_index_base tot_plot_size_both_base n_plot_both_base  epsedec  iirr  

local replace replace
foreach x in yield price{
forval i=1/4{

reg exp_`x'_other_base_ w_`x'exp_mean_base_ $contr_1 i.distr_code  $cond1 & report_`x'_exp_base_==1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}other_exp_corr.xls",  label keep(w_`x'exp_mean_base_)  ctitle(`x' - crop `i') dec(3)  `replace'
local replace

reg exp_`x'_other_base_ exp_`x'_base_ $contr_1 i.distr_code  $cond1 &report_`x'_exp_base_==1  & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}other_exp_corr.xls",  label keep(exp_`x'_base_ ) ctitle(`x' - crop `i')   dec(3)  `replace'

}
}

***********************************
**Table C.5 Reasons for not growing oilseeds at baseline
**********************************
use "${path}\dataset_crop_panel_may25" , clear

codebook d_plant_ever_base_

ge never_plant_base_= 1 -d_plant_ever_base_

bys oilseed_id: ta never_plant_base_ if  attriter==0
bys oilseed_id:  ta reas_no_grow_base_ if  attriter==0

bys oilseed_id: ta reas_no_grow_base_`i' if  attriter==0 & never_plant_base_==1, m

*****************************************
** Table C.6 Respondent and household summary statistics and balance
*****************************************
use "${path}\dataset_crop_panel_may25" , clear

global v2 tech_water tech_moisture tech_zero tech_compost tech_post_harvest  tot_rev_sales_both tot_lab_income_10k_hh  food_exp_tot_10k nonfood_exp_tot_10k gen_items_exp_tot_10k 
global cond1 "if oilseed_id==1"

foreach y of global v2{
	
	sum `y'_base $cond1 & treatment_status==0
	
	reg `y'_base treatment_status i.distr_code  $cond1, vce(cluster sub_county_code_n)
	
}

*****************************************
** Table C.7 Oilseed-specific summary statistics and balance
*****************************************
use "${path}\dataset_crop_panel_may25" ,clear

global v2  d_fert_both_   exp_fert_both_ d_pest_both_  exp_pest_both_ d_impr_seed_both_  exp_seeds_both_   
global v3 dayswork_HH_both_  dayswork_HHead_both_ dayswork_spouse_both_ dayswork_other_both_ dayswork_out_HH_both_  
global v4  actual_yields_uni_  harvest_both_ value_harv_both_ rel_value_harv_both_  
global v5  d_sale_both_   mkt_seed_ bulk_sale_   rev_sales_both_ 
global tot  $v2 $v3 $v4 $v5

local replace replace

mat drop _all

forval c=1/4{
foreach y of global tot{
local lab_`y': variable label  `y'

sum `y'base_  if treatment_status==0&oilseed_id==`c'
local avg_cont=r(mean)
local sd_contr=r(sd)

reg `y'base_  treatment_status i.distr_code  if oilseed_id==`c', cluster(sub_county_code_n)
matrix coef=(e(b))
matrix var=(e(V))

matrix lineX= nullmat(lineX) \ ( (`avg_cont'), (coef[1,1]) \ (`sd_contr') , sqrt(var[1,1]) )

}
}

drop _all
svmat lineX

local i 1
ge variables=""
order variables

foreach y of global tot{
replace variables="`lab_`y''" in `i'
local i=`i'+2
}

forvalues i=1/2{
ge nline`i'=string(lineX`i', "%9.3f") if lineX`i'<100
replace nline`i'=string(lineX`i', "%9.1f") if lineX`i' >=100

ge vnline`i'=""
}

ge Control_Mean_SD2=""
ge ITT_SE2=""

local nnn=_N

forval i=1/2{

forval h=1/`nnn'{
local gg=nline`i' in `h'
replace vnline`i'="`gg'" in `h' if mod(`h',2)==1 
replace vnline`i'="(`gg')" in `h' if mod(`h',2)==0 
}
}

keep variables vnline*
rename vnline1 Control_Mean
rename vnline2 ITT

save "${path_tables}descr_gen__cond_outcomes3", replace


************************************
**Table C.8 Project take-up
**************************************
use "${path}\dataset_crop_panel_may25" , clear

global d d_prog_takeup  n_visit_learn_plat advice_new_seed advice_oilseed_production advice_farming advice_market free_inputs_last_seas free_seeds_any_SP

local replace replace 
foreach y of global d{

sum `y'  if treatment_status==0&attriter==0&oilseed_id==1
local avg_cont=r(mean)
local sd_contr=r(sd)

reg `y'  treatment_status i.distr_code  if attriter==0 &oilseed_id==1, cluster(sub_county_code_n)
outreg2  using "${path_tables}descr_takeup_jan21.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont', SD, `sd_contr')  dec(3)
local replace 
}

************************************
**Table C.9 Program impact on yield and price expectations reporting sub-samples
**************************************
use "${path}\dataset_crop_panel_may25" , clear

global cond1 "if attriter==0"

local replace replace
foreach ff in yield price{
forval i=1/4{

sum report_`ff'_exp_base_  if treatment_status==0&attriter==0& oilseed_id==`i'
local avg_cont=r(mean)

reg report_`ff'_exp_base_  treatment_status   i.distr_code  $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}resp_itt_rep_subsamples_`ff'2.xls",  label keep(treatment_status)  adds(Control mean, `avg_cont') dec(3)  `replace'
local replace

sum rep_both_`ff'_  if treatment_status==0&attriter==0 & oilseed_id==`i'
local avg_cont=r(mean)
reg rep_both_`ff'_  treatment_status i.distr_code    $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}resp_itt_rep_subsamples_`ff'2.xls",  label keep(treatment_status )    adds(Control mean, `avg_cont') dec(3)  `replace'
local replace

sum rep_no_base_yes_end_`ff'_  if treatment_status==0&attriter==0& oilseed_id==`i'
local avg_cont=r(mean)
reg rep_no_base_yes_end_`ff'_  treatment_status  i.distr_code $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}resp_itt_rep_subsamples_`ff'2.xls",  label keep(treatment_status )   adds(Control mean, `avg_cont') dec(3)  `replace'

sum rep_yes_base_no_end_`ff'_  if treatment_status==0&attriter==0& oilseed_id==`i'
local avg_cont=r(mean)
reg rep_yes_base_no_end_`ff'_  treatment_status   i.distr_code $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}resp_itt_rep_subsamples_`ff'2.xls",  label keep(treatment_status )  adds(Control mean, `avg_cont') dec(3)  `replace'

sum rep_no_base_no_end_`ff'_  if treatment_status==0&attriter==0& oilseed_id==`i'
local avg_cont=r(mean)
reg rep_no_base_no_end_`ff'_  treatment_status  i.distr_code  $cond1 & oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}resp_itt_rep_subsamples_`ff'2.xls",  label keep(treatment_status )   adds(Control mean, `avg_cont') dec(3)  `replace'
}
}
 

************************************
* Tab C.10 Program impact on levels of yield and price expectations (intensive margin), Lee Bounds
************************************

**Panel A: main effects

use "${path}\dataset_crop_panel_may25" , clear

global cond1 "if attriter==0"

mat drop _all

local replace replace 
forval i=1/4{
	bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_yieldexp_mean_ treatment_status $cond1 & oilseed_id==`i', select(report_yield_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

matrix LB=nullmat(LB) , ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') )

}

*pooled
bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_yieldexp_mean_NR_ treatment_status $cond1, select(report_yield_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

matrix LB=LB, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') )

restore

*price 
forval i=1/4{

bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_priceexp_mean_ treatment_status $cond1 & oilseed_id==`i' , select(report_price_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

matrix LB=LB, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') )
}

*pooled

bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_priceexp_mean_ treatment_status $cond1, select(report_price_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

matrix LB=LB, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') )

restore

drop _all
svmat LB

local i 1
ge variables=""
order variables

forvalues i=1/10{
ge nline`i'=string(LB`i', "%9.3f") 
ge vnline`i'=""
}

local nnn=_N

forval i=1/10{

forval h=1/`nnn'{
local gg=nline`i' in `h'
replace vnline`i'="`gg'" in `h' if mod(`h',2)==1 
replace vnline`i'="(`gg')" in `h' if mod(`h',2)==0 
}
}

keep variables vnline*

****Panel B: Heterogeneous effects
use "${path}\dataset_crop_panel_may25" , clear

global cond1 "if attriter==0"

replace w_yieldexp_mean_base_=0 if report_yield_exp_base_`i'==0
replace w_priceexp_mean_base_=0 if report_price_exp_base_`i'==0

mat drop _all

local replace replace 
forval i=1/4{

*optimist	
	bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_yieldexp_mean_ treatment_status $cond1 & optimist_yield_base_==1 & oilseed_id==`i', select(report_yield_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB1=nullmat(LB1) , ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') \ (`sample') )

*pessimists
	bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_yieldexp_mean_ treatment_status $cond1 & pessimist_yield_base1_`i'==1 & oilseed_id==`i', select(report_yield_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB2=nullmat(LB2) , ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse')\ (`sample') )

}

bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_yieldexp_mean_NR_ treatment_status $cond1 &optimist_yield_base1_==1, select(report_yield_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB1=LB1, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') \ (`sample') )

bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_yieldexp_mean_NR_ treatment_status $cond1 & pessimist_yield_base1_==1, select(report_yield_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB2=LB2, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse')\(`sample') )


*price 
forval i=1/4{
*optimist
bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_priceexp_mean_ treatment_status $cond1 & optimist_price_base1_==1 & oilseed_id==`i', select(report_price_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB1=LB1, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') \(`sample') )

*pessimist
bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_priceexp_mean_NR_ treatment_status $cond1 & pessimist_price_base1_==1 & oilseed_id==`i' , select(report_price_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB2=LB2, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') \(`sample') )
}

*optimist
bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_priceexp_mean_NR_ treatment_status $cond1 & optimist_price_base1_==1, select(report_price_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB1=LB1, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') \(`sample') )

*pessimist
bootstrap lb=_b[lower] ub=_b[upper], reps(500) cluster(sub_county_code_n): leebounds w_priceexp_mean_NR_ treatment_status $cond1 & pessimist_price_base1_==1, select(report_price_exp_)

local llb=e(b)[1,1]
local uub=e(b)[1,2]

local llbse=e(se)[1,1]
local uubse=e(se)[1,2]

local sample=e(N)

matrix LB2=LB2, ( (`uub') \ ( `uubse') \ (`llb') \ ( `llbse') \(`sample') )


forval v=1/2{
drop _all

svmat LB`v'

ge variables=""
order variables

forvalues i=1/10{
ge nline`i'=string(LB`v'`i', "%9.3f")  if LB`v'`i' <10
replace nline`i'=string(LB`v'`i', "%9.0f") if LB`v'`i' >=10
ge vnline`i'=""
}

local nnn=_N

forval i=1/10{

forval h=1/`nnn'{
local gg=nline`i' in `h'
replace vnline`i'="`gg'" in `h' if mod(`h',2)==1 
replace vnline`i'="(`gg')" in `h' if mod(`h',2)==0 
}
}

keep variables vnline*

save LB`v', replace
}


************************************
* Tab C.11 Program impact on the reasons for not growing oilseeds at the endline
************************************
use "${path}\dataset_crop_panel_may25"  ,clear

ta reas_no_grow_ , gen(reas_no_grow_)
ta reas_no_grow_base_ , gen(reas_no_grow_base_)

ge d_never_plant=1-d_plant_ever_
ge d_never_plant_base_=1-d_plant_ever_base_

local replace replace
sum d_never_plant  if treatment_status==0&attriter==0
local avg_cont=r(mean)

reg d_never_plant treatment_status d_never_plant_base_  i.distr_code i.oilseed_id if attriter==0  , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_reas2.xls", `replace' keep(treatment_status) label    adds(Control mean, `avg_cont') dec(3)
local replace 

cap drop miss_end_reas
ge miss_end_reas=missing(reas_no_grow_) & d_plant_ever_==0

sum miss_end_reas if treatment_status==0&attriter==0
local avg_cont=r(mean)

reg miss_end_reas treatment_status  i.distr_code i.oilseed_id if attriter==0 , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_reas2.xls", `replace' keep(treatment_status) label     adds(Control mean, `avg_cont') dec(3)

forval i=1/4{
sum reas_no_grow_ if treatment_status==0&attriter==0& oilseed_id==`i'
local avg_cont=r(mean)

reg reas_no_grow_ treatment_status reas_no_grow_base_`i'   i.distr_code i.oilseed_id if attriter==0& d_plant_ever_base_==0&oilseed_id==`i', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_reas2.xls", `replace' keep(treatment_status) label     adds(Control mean, `avg_cont') dec(3)
}


************************************
* Tab C.12 Program impact on other crops
************************************
use "${path}\dataset_crop_panel_may25" , clear

global cond1 "if attriter==0"

global v8 d_cereals_both sh_area_cereals_both d_legumes_both sh_area_legumes_both ///
d_vegetables_both sh_area_vegetables_both d_plantation_both sh_area_plantation_both d_tubers_both sh_area_tubers_both d_fruits_both sh_area_fruits_both d_spices_both sh_area_spices_both

	local replace replace

foreach y of global v8{
	
sum `y'  if treatment_status==0&attriter==0 &oilseed_id==1
local avg_cont=r(mean)

reg `y' treatment_status `y'_base  i.distr_code if attriter==0&oilseed_id==1  , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}other_crops.xls", `replace' keep(treatment_status) label    adds(Control mean, `avg_cont') dec(3)
local replace 
	
}

************************************
* Tab C.13 Program impact on use of inputs for oilseeds
************************************
use "${path}\dataset_crop_panel_may25" ,clear

global v2  d_fert_both_   exp_fert_both_ d_pest_both_  exp_pest_both_ d_impr_seed_both_  exp_seeds_both_  index_klk_input_ 

local cond1 "if attriter==0"

local replace replace
forval u=1/4{

foreach y in $v2{ 

sum `y'  $cond1 & treatment_status==0 & oilseed_id  ==`u'
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code `cond1' & oilseed_id  ==`u' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_inputs.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace
}
}

local cond1 "if attriter==0"

foreach y in $v2{ 

sum `y'  $cond1 & treatment_status==0
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code i.oilseed_id `cond1' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_inputs.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
}

************************************
* Tab C.14 Program impact on agricultural practices
************************************
use "${path}\dataset_crop_panel_may25" ,clear

global cond1 "if attriter==0& oilseed_id==1"

global v1  d_fertil_both d_pestic_both d_impr_seed_both tech_water tech_moisture tech_zero tech_compost tech_post_harvest index_klk_input_all

local replace replace 
foreach y in $v1{ 

sum `y'  $cond1 & treatment_status==0 & oilseed_id==1
local avg_cont=r(mean)

reg `y'   treatment_status   `y'_base   i.distr_code  $cond1  & oilseed_id==1 , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_pract.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace
}

************************************
* Tab C.15 Program impact on labour allocation for oilseeds
************************************
use "${path}\dataset_crop_panel_may25" ,clear

global v3 dayswork_HH_both_  dayswork_HHead_both_ dayswork_spouse_both_ dayswork_other_both_ dayswork_out_HH_both_ index_klk_work_ 

local cond1 "if attriter==0"

local replace replace
forval u=1/4{

foreach y in $v3{ 

sum `y' `cond1' & treatment_status==0 & oilseed_id  ==`u'
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code `cond1' & oilseed_id  ==`u' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_labour.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace
}
}

local replace replace
local cond1 "if attriter==0"

foreach y in $v3{ 

sum `y'   `cond1'  & treatment_status==0
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code i.oilseed_id `cond1' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_labour2.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace 
}

************************************
* Tab C.16 Program impact on oilseeds production and productivity
************************************
use "${path}\dataset_crop_panel_may25" ,clear

global v4  actual_yields_uni_  harvest_both_ value_harv_both_ rel_value_harv_both_  index_klk_prod_ 

local replace replace
forval u=1/4{
local cond1 "if attriter==0"

foreach y in $v4{ 

sum `y' `cond1' & treatment_status==0 & oilseed_id  ==`u'
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code `cond1' & oilseed_id  ==`u' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_yield.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace
}
}

local replace replace
local cond1 "if attriter==0"

foreach y in $v4{ 

sum `y'   `cond1'  & treatment_status==0
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code i.oilseed_id `cond1' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_labour2.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace 
}

************************************
* Tab C.17 Program impact on oilseeds market linkages
************************************
use "${path}\dataset_crop_panel_may25" ,clear

global v5  d_sale_both_   mkt_seed_ bulk_sale_ rev_sales_both_ index_klk_sale_ 

local replace replace
forval u=1/4{
local cond1 "if attriter==0"

foreach y in $v5{ 

sum `y' `cond1' & treatment_status==0 & oilseed_id  ==`u'
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code `cond1' & oilseed_id  ==`u' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_sale.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace
}
}

local cond1 "if attriter==0"
foreach y in $v5{ 

sum `y'   `cond1'  & treatment_status==0
local avg_cont=r(mean)

reg `y'   treatment_status   `y'base_   i.distr_code i.oilseed_id `cond1' , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_sale.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace 
}

************************************
* Tab C.18 Program impact on household welfare
************************************
use "${path}\dataset_crop_panel_may25" ,clear

global v2 tot_rev_sales_both tot_lab_income_10k_hh wealth_index  food_exp_tot_10k nonfood_exp_tot_10k gen_items_exp_tot_10k index_klk_welfare

global cond1 "if attriter==0 & oilseed_id==1"


local replace replace 
foreach y in $v2{ 

sum `y'  $cond1 & treatment_status==0 & oilseed_id==1
local avg_cont=r(mean)

reg `y'   treatment_status   `y'_base   i.distr_code  $cond1 , vce(cluster sub_county_code_n)
outreg2  using "${path_tables}itt_welfare.xls", `replace' keep(treatment_status) label   adds(Control mean, `avg_cont') dec(3)
local replace
}

************************************
* Tab C.18 - C23 Program heterogeneous effects 
************************************

use "${path}\dataset_crop_panel_may25"  ,clear

local het1  tot_plot_size_both_base  index_klk_input_all tot_lab_income_10k_hh wealth_index_base 
local het2 d_plant_ever_base_  head_male_base 

local replace replace

forval u=1/4{
foreach y in   d_adopt_both_  sh_adopt_uni_  actual_yields_uni_  value_harv_both_ rev_sales_both_ {
local lab_`y': variable label  `y'

*continuous het
foreach h of local het1{
 local lab_`h': variable label  `h'
cap drop het
clonevar het= `h'
la var het "Het"

reg `y'   treatment_status##c.het  `y'base_  i.distr_code  if attriter==0 & oilseed_id==`u', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}het_PAP_`y'.xls", `replace' keep(1.treatment_status het 1.treatment_status##c.het  ) label ctitle("`lab_`y'',`lab_`h'',`u'")   dec(3)
local replace append
}

*discrete het
foreach h of local het2{
 local lab_`h': variable label  `h'
cap drop het
clonevar het= `h'
la var het "Het"

reg `y'   treatment_status##het   `y'base_  i.distr_code if attriter==0 & oilseed_id==`u', vce(cluster sub_county_code_n)
outreg2  using "${path_tables}het_PAP_`y'.xls", `replace' keep(1.treatment_status het 1.treatment_status##1.het  ) ///
label ctitle("`lab_`y'',`lab_`h'',`u'")   dec(3)
}
}
}

************************************
* Tab C.24 Program heterogeneous effects on oilseed adoption, production and market linkages
************************************

***pooled model with MHT
use "${path}\dataset_crop_panel_may25"  ,clear

mat drop _all
local het1  tot_plot_size_both_base  index_klk_input_all tot_lab_income_10k_hh wealth_index_base 
local het2 d_plant_ever_base_  head_male_base 

local replace replace

foreach y in   index_klk_adoption_  index_klk_prod_  index_klk_sale_{
local lab_`y': variable label  `y'

*continuous het
foreach h of local het1{
 local lab_`h': variable label  `h'
cap drop het
clonevar het= `h'
la var het "Het"

reg `y'   treatment_status##c.het  `y'base_  i.distr_code i.oilseed_id if attriter==0 , vce(cluster sub_county_code_n)
test 1.treatment_status#het

matrix p2=nullmat(p2)\(r(p))

outreg2  using "${path_tables}het_PAP2.xls", `replace' keep(1.treatment_status het 1.treatment_status##c.het  ) label ctitle("`lab_`y'',`lab_`h''")   addt(Controls, Yes, Strata FE, Yes, Crop FE, Yes)  dec(3)
local replace append
}

*discrete het
foreach h of local het2{
 local lab_`h': variable label  `h'
cap drop het
clonevar het= `h'
la var het "Het"

reg `y'   treatment_status##het   `y'base_  i.distr_code i.oilseed_id if attriter==0, vce(cluster sub_county_code_n)
test 1.treatment_status#1.het
matrix p2=nullmat(p2)\(r(p))

outreg2  using "${path_tables}het_PAP2.xls", `replace' keep(1.treatment_status het 1.treatment_status##1.het  ) ///
label ctitle("`lab_`y'',`lab_`h''")   addt(Controls, Yes, Strata FE, Yes, Crop FE, Yes)  dec(3)
}
}


drop _all
svmat p2
rename p2 pval


preserve
keep in 1/6
qqvalue  pval , me(hochberg ) qv(q_val_index1)
format pval q_val_index1 %9.3f

save ${path_tables}pval1, replace
restore 

preserve
keep in 7/12
qqvalue  pval , me(hochberg ) qv(q_val_index2)
format pval q_val_index2 %9.3f

save ${path_tables}pval2, replace
restore 

preserve
keep in 13/18
qqvalue  pval , me(hochberg ) qv(q_val_index3)
format pval q_val_index3 %9.3f

save ${path_tables}pval3, replace
restore 

